package com.myproject.http;


import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpClient {

    public static void main(String[] args) throws Exception {

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost("http://localhost:9960/qd/release");
        //String jsonString = "{\"reqId\":\"adjfadjflajgeogjadfj\",\"reqParams\":{\"user_id\":\"+VMwUN74zzudFIideCI/Q==\",\"act_no\":\"012020050918347\",\"act_nm\":\"2020年零售场景券点铺底活动\",\"trans_tm\":\"20200910123129\",\"cups_pk\":\"1234_20200820123129_357_754\",\"issue_at\":\"40\"}}";
        //String jsonString = "{\"reqId\":\"adjfadjflajgeogjadfj\"}";
        String jsonString = "王重阳";

        StringEntity entity = new StringEntity(jsonString, "UTF-8");
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        // 响应模型
        CloseableHttpResponse response = null;
        // 由客户端执行(发送)Post请求
        response = httpClient.execute(httpPost);
        // 从响应模型中获取响应实体
        HttpEntity responseEntity = response.getEntity();

        System.out.println("响应状态为:" + response.getStatusLine());
        if (responseEntity != null) {
            System.out.println("响应内容长度为:" + responseEntity.getContentLength());
            System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
        }

        if (httpClient != null) {
            httpClient.close();
        }
        if (response != null) {
            response.close();
        }



    }
}
